<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML" 4.01"//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- Created: August 23, 2001-->
<!--
$Id: RepGenDataS.php,v 1.3 2001/11/02 19:08:00 eugenem Exp eugenem $
$Header: /export/home/eugenem/public_html/ComplianceReports/RCS/RepGenDataS.php,v 1.3 2001/11/02 19:08:00 eugenem Exp eugenem $
$Log: RepGenDataS.php,v $
Revision 1.3  2001/11/02 19:08:00  eugenem
Moved HTTP_*_VARS processing to dbc.inc

Revision 1.2  2001/11/02 14:58:50  eugenem
Moved database connect statements to a separate file.

Revision 1.1  2001/09/04 18:45:22  eugenem
Initial revision

-->
<!--
Retrieve and display generator check data by survey
-->
<html>
<head>
<title>Radiological Equipment Database</title>
<meta name="author" content="Eugene Mah">
<link rev="made" href="mailto:eugenem@ix.netcom.com">
<link rev="home" href="http://home.netcom.com/~eugenem/">
<meta name="description" content="">
<meta name="Keywords" content="">
<meta name="Reply-to" content="eugenem@ix.netcom.com">
<META http-equiv="PICS-Label" content='(PICS-1.1 "http://www.classify.org/safesurf/" l r (SS~~0001))'>
<?PHP
require('functions.inc');
require('dbc.inc');

/*
  Define constants that indicate what each line is used for
*/
define("LINEARITY", 1);  // Low bit
define("ACCURACY", 2);
define("BEAMQUAL", 4);
define("REPRO", 8);  // High bit

?>
</head>

<body>
<h1>Radiological Equipment Database</h1>
<?PHP
if (isset($SurveyID)) {
  /* Get machine info for the selected survey */
  $query = "SELECT Description,TestDate
            FROM Machines,TestDates 
			WHERE Machines.MachineID=TestDates.MachineID 
			AND TestDates.SurveyID=$SurveyID";
  $result = mysql_query($query)
    or die("<p>Unable to execute query: $query<br />" . mysql_error() . "</p>");
  $Machine = mysql_fetch_array($result);
  mysql_free_result($result);

  // Count the number of tubes entered for this survey
  $query = "SELECT DISTINCT SurveyID,TubeID FROM GenData WHERE SurveyID=$SurveyID";
  $tubes = mysql_query($query)
    or die("<p>Unable to execute query: $query<br />" . mysql_error() . "</p>");
  echo "<h2>Generator check data for " . $Machine['Description'] . " (SurveyID $SurveyID on " . $Machine['TestDate'] . ")</h2>\n";

  while ($t = mysql_fetch_row($tubes)) {
    $TubeID = $t[1];  // Get the current TubeID

    // Get info on the tubes found
    $query = "SELECT Manufacturer AS HsgManuf,HousingModel,HousingSN,Notes
	          FROM Tubes,Manufacturer WHERE HousingManufID=ManufacturerID
			  AND TubeID=$TubeID";
    $result = mysql_query($query)
      or die("<p>Unable to execute query: $query<br />" . mysql_error() . "</p>");
    $tinfo = mysql_fetch_assoc($result);
    mysql_free_result($result);

    printf("<h3>%s Model %s SN %s (%s)</h3>\n",$tinfo['HsgManuf'],$tinfo['HousingModel'],$tinfo['HousingSN'],$tinfo['Notes']);

    // Determine kV, exposure, timer reproducibility
    /*
    ** LFS reproducibility
    */
    $query = "SELECT ROUND(AVG(kVEff),2),ROUND(STDDEV(kVEff),2),ROUND(AVG(Exp),2),ROUND(STDDEV(Exp),2),ROUND(AVG(ExpTime),2), ROUND(STDDEV(ExpTime),2) 
			  FROM GenData 
			  WHERE SurveyID=$SurveyID 
			  AND (UseFlags & " . REPRO . ") AND (kVSet=80 or kVSet=81) 
			  AND TubeID=$TubeID";
    $result = mysql_query($query)
      or die("<p>Unable to execute query: $query<br />" . mysql_error() . "</p>");
    $lrepro = mysql_fetch_row($result);
    mysql_free_result($result);

    /*
    ** SFS reproducibility
    */
    $query = "SELECT ROUND(AVG(kVEff),2),ROUND(STDDEV(kVEff),2),ROUND(AVG(Exp),2),ROUND(STDDEV(Exp),2),ROUND(AVG(ExpTime),2),round(STDDEV(ExpTime),2) 
	          FROM GenData 
			  WHERE SurveyID=$SurveyID 
			  AND (UseFlags & " . REPRO . ") AND kVSet=90 
			  AND TubeID=$TubeID";
    $result = mysql_query($query)
      or die("<p>Unable to execute query: $query<br />" . mysql_error() . "</p>");
    $srepro = mysql_fetch_row($result);
    mysql_free_result($result);

    echo "<h3>kVp and Exposure Reproducibility</h3>\n";
    echo "<table>\n";
    echo "<tr>\n";
    echo "<th></th><th>Large focal spot</th><th>Small focal spot</th>\n";
    echo "</tr>\n<tr align=center>\n";  
    echo "<td>Mean kVp:</td>\n";
    echo "<td>" . $lrepro[0] . "</td>\n";
    echo "<td>" . $srepro[0] . "</td>\n";
    echo "</tr>\n<tr align=center>\n";  
    echo "<td>Standard Deviation:</td>\n";
    echo "<td>" . $lrepro[1] . "</td>\n";
    echo "<td>" . $srepro[1] . "</td>\n";
    echo "</tr>\n<tr align=center>\n";
    echo "<td>Coeff of Var:</td>\n";
    echo "<td>" . round(cv($lrepro[0],$lrepro[1]),2) . "%</td>\n";
    echo "<td>" . round(cv($srepro[0],$srepro[1]),2) . "%</td>\n";
    echo "</tr>\n<tr align=center>\n";
    echo "<td>Mean Exposure (mR):</td>\n";
    echo "<td>" . $lrepro[2] . "</td>\n";
    echo "<td>" . $srepro[2] . "</td>\n";
    echo "</tr>\n<tr align=center>\n";  
    echo "<td>Standard Deviation:</td>\n";
    echo "<td>" . $lrepro[3] . "</td>\n";
    echo "<td>" . $srepro[3] . "<td>\n";
    echo "</tr>\n<tr align=center>\n";  
    echo "<td>Coeff of Var:</td>\n";
    echo "<td>" . round(cv($lrepro[2],$lrepro[3]),2) . "%</td>\n";
    echo "<td>" . round(cv($srepro[2],$srepro[3]),2) . "%</td>\n";
    echo "</tr>\n<tr align=center>\n";  
    echo "<td>Mean Exp time (s):</td>\n";
    echo "<td>" . $lrepro[4] . "</td>\n";
    echo "<td>" . $srepro[4] . "</td>\n";
    echo "</tr>\n<tr align=center>\n";  
    echo "<td>Standard Deviation:</td>\n";
    echo "<td>" . $lrepro[5] . "</td>\n";
    echo "<td>" . $srepro[5] . "</td>\n";
    echo "</tr>\n<tr align=center>\n";  
    echo "<td>Coeff of Var:</td>\n";
    echo "<td>" . round(cv($lrepro[4],$lrepro[5]),2) . "%</td>\n";
    echo "<td>" . round(cv($srepro[4],$srepro[5]),2) . "%</td>\n";
    echo "</tr>\n";  
    echo "</table>";
    echo "<hr width=50% />";

    /*
    ** Determine kV,timer Accuracy
    */
    $query = "SELECT kVSet,kVEff,kVMax,Timeset,ExpTime 
	          FROM GenData 
			  WHERE SurveyID=$SurveyID 
			  AND (UseFlags & " . ACCURACY . ") 
			  AND TubeID=$TubeID 
			  ORDER BY kVSet";
    $result = mysql_query($query)
      or die("<p>Unable to execute query: $query<br />" . mysql_error() . "</p>");

    echo "<h3>kVp and Timer Accuracy</h3>\n";
    echo "<table>\n";
    echo "<th>Set kV</th><th>kV eff</th><th>kV max</th><th>kV<br>error</th><th>Set time</th><th>Time</th><th>Time error</th>\n";
    while ($row = mysql_fetch_row($result)) {
      echo "<tr align=center>\n";
      echo "<td>" . $row[0] . "</td>";
      echo "<td>" . $row[1] . "</td>";
      echo "<td>" . $row[2] . "</td>";
      echo "<td>";
      if ($row[2]<>0) echo round((($row[2]-$row[0])/$row[0])*100,2);
      else echo round((($row[1]-$row[0])/$row[0])*100,2);
      echo "%</td>";
      echo "<td>" . $row[3] . "</td>";
      echo "<td>" . $row[4] . "</td>";
      echo "<td>";
      if ($row[4]<>0) echo round((($row[4]-$row[3])/$row[3])*100,2);
      else echo "0";
      echo "%</td>\n";
      echo "<tr>\n";
    }
    echo "</table>\n";
    echo "<hr width=50%/>";
    mysql_free_result($result);

    /*
    ** Determine mA/exposure linearity
    */
    $query = "SELECT mAset,mAsset,Timeset,ROUND(mAset*Timeset,2) AS mAs,Exp,
	          IF(mAsset,ROUND(Exp/mAsset,2),ROUND(Exp/(mAset*Timeset),2)) AS mRmAs 
			  FROM GenData 
			  WHERE TubeID=$TubeID 
			  AND SurveyID=$SurveyID 
			  AND (UseFlags & " . LINEARITY . ") 
			  ORDER BY mAset";
    $result = mysql_query($query)
      or die("<p>Unable to execute query: $query<br />" . mysql_error() . "</p>");

    $max = 0;
    $min = 1000;

    echo "<h3>Exposure/mA Linearity</h3>\n";
    echo "<table>\n";
    echo "<tr><th>Set mA</th><th>Set mAs</th><th>Set Time</th><th>mAs</th><th>Exposure</th><th>mR/mAs</th></tr>\n";
    while ($row = mysql_fetch_row($result)) {
      if ($row[4]>0) {
        $max = ($row[5]>$max)?$row[5]:$max;
        $min = ($row[5]>$min)?$min:$row[5];
        echo "<tr align=center>\n";
        echo "<td>" . $row[0] . "</td>\n";
        echo "<td>" . $row[1] . "</td>\n";
        echo "<td>" . $row[2] . "</td>\n";
        echo "<td>" . $row[3] . "</td>\n";
        echo "<td>" . $row[4] . "</td>\n";
        echo "<td>" . $row[5] . "</td>\n";
        echo "</tr>\n";
      }
    }
    echo "</table>\n";
    echo "<p>Max mR/mAs: $max<br>\n";
    echo "Min mR/mAs: $min<br>\n";
    echo "Overall linearity: " . round(($max - $min)/($max + $min),3) * 100 . "%</p>\n";
    echo "<hr width=50% />";
    mysql_free_result($result);

    /*
    ** Determine HVL
    */
    $query = "SELECT ROUND(AVG(kVEff),2),ROUND(AVG(Exp),2),AddFilt 
	          FROM GenData 
			  WHERE SurveyID=$SurveyID 
			  AND (UseFlags & " . BEAMQUAL . ") 
			  AND TubeID=$TubeID 
			  GROUP BY AddFilt 
			  ORDER BY AddFilt";
    $result = mysql_query($query)
      or die("<p>Unable to execute query: $query<br />" . mysql_error() . "</p>");

    $row = mysql_fetch_row($result);  // Get the results for no attenuator
    $kVp_0 = $row[0];
    $Exp_0 = $row[1];
    $Thick_0 = $row[2];
    while ($row = mysql_fetch_row($result)) {
      if ($row[1] >= $Exp_0/2) {
        $Exp_a = $row[1];  // Exposure just above half
        $kVp_a = $row[0];
        $Thick_a = $row[2];  // Thickness just above half
      }
      if (($row[1] < $Exp_0/2) && ($row[1] > 0.0)) {
        $Exp_b = $row[1];  // Exposure just below half
        $kVp_b = $row[0];
        $Thick_b = $row[2];  // Thickness just above half
        break 1;
      }
    }

    $HVL = ($Thick_b * log(2*$Exp_a/$Exp_0) - $Thick_a * log(2*$Exp_b/$Exp_0)) / log($Exp_a/$Exp_b);

    /* Spit out the results into a table */
    echo "<h3>Half Value Layer</3>\n";
    echo "<table>\n";
    echo "<tr><th>Filtration</th><th>Avg Exp</th><th>Avg kVp</th></tr>\n";
    echo "<tr><td>$Thick_0</td><td>$Exp_0</td><td>$kVp_0</td></tr>\n";
    echo "<tr><td>$Thick_a</td><td>$Exp_a</td><td>$kVp_a</td></tr>\n";
    echo "<tr><td>$Thick_b</td><td>$Exp_b</td><td>$kVp_b</td></tr>\n";
    echo "</table>\n";
    echo "<p><b>HVL = " . round($HVL,2) . " mm Al</b></p>\n";
    echo "<hr width=50% />";
  
    mysql_free_result($result);

    echo "<h2>Generator check data</h2>\n";

    /* Get the generator check data for the selected survey */
    $query = "SELECT * FROM GenData WHERE SurveyID=$SurveyID AND TubeID=$TubeID";
    $result = mysql_query($query)
      or die("<p>Unable to execute query: $query<br />" . mysql_error() . "</p>");

    /*
      Loop through the results contained in $GenData.
    */
    echo "<table cellpadding=5 border=1>\n";
    echo "<tr>\n";
    echo "<th>ID</th><th>Survey</th><th>Tube</th><th>Set kVp</th><th>Set mA</th><th>Set Time</th><th>Set mAs</th><th>Filtration</th><th>Distance</th><th>Avg kVp</th><th>Eff kVp</th><th>Max kVp</th><th>Exp time (s)</th><th>Exposure (mR)</th><th>Flags</th>\n";
    while ($GenData = mysql_fetch_row($result)) {
      echo "<tr>\n";
      while (list($name,$val) = each($GenData)) {
        echo "<td>$val</td>\n";
      }
      echo "</tr>\n";
    }
    echo "</table>\n";
    echo "<hr />\n";
    
    // Free up memory used by query results
    mysql_free_result($result);
  }
}
?>
<form action="RepGenDataS.php" method="POST">
<p>Select a survey:
<?PHP
$survID = mysql_query("SELECT DISTINCT SurveyID FROM GenData ORDER BY SurveyID");
echo listbox_one_field($survID,"SurveyID","");
?>
</p>
<p>
<input type="SUBMIT" value="Retrieve data"> / <input type="RESET" value="Reset"> / <a href="index.html" target="_top" >Main</a>
</p>
</form>
<?PHP mysql_close(); ?>
</body>
</html>
